package com.example.springboothomework.dao;

import com.example.springboothomework.entity.Employee;
import com.example.springboothomework.entity.excel.EmployeeExcelExport;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author: chenjianhao
 * @date: 2025/6/5
 * @description: 员工Repository
 */
@Repository
public interface EmployeeDao extends JpaRepository<Employee, Long>, QuerydslPredicateExecutor<Employee> {

    /**
     * 根据公司id判断是否存在员工
     * @return
     */
    Boolean existsByCompanyId(Long companyId);


    /**
     *  lastId后1000条，按照id升序排序
     * @param lastId
     * @param pageRequest
     * @return
     */
    @Query("SELECT NEW com.example.springboothomework.entity.excel.EmployeeExcelExport" +
            " ( e.id, e.name,  e.age,e.employmentDate, e.employed, c.name) " +
            "FROM Employee e " +
            "LEFT JOIN Company c ON e.companyId = c.id " +
            "WHERE e.id > :lastId " +
            "AND (:searchKeyword IS NULL OR e.name LIKE %:searchKeyword%)"+
            "AND (:companyId IS NULL OR e.companyId = :companyId) " +
            "ORDER BY e.id ASC")
    List<EmployeeExcelExport> findByIdThanOrderByIdAscAndFilters(Long lastId, String searchKeyword, Long companyId, PageRequest pageRequest);
}
